package com.ht.pms.core.util;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ht.pms.core.dto.Doc;
import com.ht.pms.core.dto.dingding.SendDingdingMsgVo;
import com.ht.pms.core.service.DocService;
import com.ht.ussp.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhouhs on 2017/1/10.
 */
@Service
public class WordAction {

    private final static Logger LOG = LoggerFactory.getLogger(WordAction.class);

    @Value("${pms.filePath}")
    private String filePath;

    @Autowired
    private DocService docService;

    @Autowired
    private NorthRequest northRequest;

    @Value("${pms.templetBase}")
    private String templetBase;

    public String createWord(SendDingdingMsgVo data, Integer type) throws Exception {
        /** 文件名称，唯一字符串 */

        //文件路径
//        filePath = "D:/approveBills/";

        //文件唯一名称
        String fileOnlyName = data.getApplyUser()+"的"+data.getDocumentType()+"报销单" + data.getApproveId() + ".doc";

        String  fullFilePath = null;

        Doc doc = docService.selectOne(new EntityWrapper<Doc>().eq("business_id","template").eq("doc_type_id",type));
        if(doc != null){
            File tempFile = northRequest.downLoadFromUrl(doc.getFullUrl(),doc.getDocName(),templetBase);
            /** 生成word */
            fullFilePath =WordUtil.createWord(JsonUtil.obj2Map(data), templetBase,doc.getDocName(), filePath, fileOnlyName);

        }else {
            LOG.info("模板id【{}】",type);
            throw new Exception("模板未找到");
        }

        return fullFilePath;
    }



    public String createWordTest() throws Exception{
        /** 用于组装word页面需要的数据 */
        Map<String, Object> dataMap = new HashMap<String, Object>();

        dataMap.put("companyName","广东汉升软件科技有限公司佛山分公司");
        dataMap.put("expenseDepartment","行政人事部");
        dataMap.put("approveTime","2017/6/22");
        dataMap.put("approveId","BX-20170622013");
        dataMap.put("payee","公积金局321321硕大的撒多撒大多撒大所大所大所大所大所大所多撒大所大所大所大所大所");
        dataMap.put("attachNum","2");
        dataMap.put("totalAmountBig","壹万叁仟肆佰捌拾捌元整");
        dataMap.put("totalAmount","13488.00 ");
        dataMap.put("applyUser","张大");
        dataMap.put("approveOne","张三");
        dataMap.put("approveTwo","李四");
        dataMap.put("approveThree","王五");
        dataMap.put("approveFour","王五");
        dataMap.put("approveFive","王五");
        dataMap.put("approveSix","王五");
        dataMap.put("approveSeven","王五");
        dataMap.put("approveEight","王五");
        dataMap.put("approveNine","王五");
        dataMap.put("approveOneTitle","审批岗位");
        dataMap.put("approveTwoTitle","审批岗位");
        dataMap.put("approveThreeTitle","审批岗位");
        dataMap.put("approveFourTitle","审批岗位");
        dataMap.put("approveFiveTitle","审批岗位");
        dataMap.put("approveSixTitle","审批岗位");
        dataMap.put("approveSevenTitle","审批岗位");
        dataMap.put("approveEightTitle","审批岗位");
        dataMap.put("approveNineTitle","审批岗位");
        dataMap.put("documentType","费用报销单");

        List<Map<String,String>> mapList = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("index","1");
        map.put("feeName","测试");
        map.put("abstractNote","测试摘要");
        map.put("amount","1312");
        map.put("payType","现金");
        mapList.add(map);

        Map<String,String> map6 = new HashMap<>();
        map6.put("index","2");
        map6.put("feeName","测试");
        map6.put("abstractNote","测试摘要");
        map6.put("amount","1312");
        map6.put("payType","现金");
        mapList.add(map6);

        Map<String,String> map1 = new HashMap<>();
        map1.put("index","3");
        map1.put("feeName","测试");
        map1.put("abstractNote","测试摘要");
        map1.put("amount","1312");
        map1.put("payType","现金");
        mapList.add(map1);

        Map<String,String> map2 = new HashMap<>();
        map2.put("index","4");
        map2.put("feeName","测试");
        map2.put("abstractNote","测试摘要");
        map2.put("amount","1312");
        map2.put("payType","现金");
        mapList.add(map2);


        dataMap.put("applyReimbursementDetails",mapList);

        /** 文件名称，唯一字符串 */
        Random r = new Random();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        StringBuffer sb = new StringBuffer();
        sb.append(sdf1.format(new Date()));
        sb.append("_");
        sb.append(r.nextInt(100));

        //文件路径
        String filePath = "D:/testExcel/";

        //文件唯一名称
        String fileOnlyName = "用freemarker生成excel文档_" + sb + ".doc";


        /** 生成word */
        WordUtil.createWordTest(dataMap, "template/wordTemplateNew2.ftl", filePath, fileOnlyName);

        return "createWordSuccess";
    }

    public static void main(String[] args) throws Exception{
        WordAction wordAction = new WordAction();
        System.out.println(wordAction.createWordTest());
//        Integer type = Integer.parseInt("1");
//        System.out.println(ApplyFileEnum.getFileNameByCode(type));

    }





}
